import axios from "axios"
import { forEachChild } from "typescript";
import store from '../store/store'
import { toast } from "./toast"
const instance = axios.create({
  baseURL: process.env.NODE_ENV==='production'?'//rank.jasonandjay.com':'',
  timeout: 1000,
});
const erros = {
  400: '客户端请求错误',
  401: '请求未经授权',
  404: '未找到资源',
  500: '服务器内部错误',
  502: '网关错误',
  503: '服务不可用',
  504: '超时'
}
instance.interceptors.request.use(function (config) {
  store.loading.showFlag()
  return config;
}, function (error) {
  console.log(error);
  return Promise.reject(error);
});

instance.interceptors.response.use(function (response) {
  store.loading.hashFlag()
  if (response.status !== 200) {
    console.log(response);
    toast(erros[response.status]);
    return;
  }
  // 2. 处理业务逻辑错误
  if (response.data.result !== '0') {
    console.log(response);
    toast(response.data.resultNote);
    return;
  }
  return response;
}, function (error) {
  // store.loading.hashFlag();
  console.log(error);
  toast(error.toString());
  return Promise.reject(error);
});
export default instance